<?php

class ContractReportForm extends CFormModel{
    public $startDate;
    public $endDate;
    public $status;
    public $type;
    public $empno;
    public function rules() {
        return array(
            array('startDate,endDate', 'required'),
            array('startDate,endDate', 'date', 'format' => 'yyyy-MM-dd'),
            array('type,status','safe'),
            array('empno','exist','className'=>'Employees','attributeName'=>'empno')
        );
    }
    public function getEmployeeList(){
        $list=Employees::model()->getEmployeeList();
        $list[NULL]='All';
        return $list;
    }
    public function getTypeList(){
        $list=ContractType::getListData();
        $list[NULL]='All';
        return $list;
    }
    public function getStatusList() {
        $list=  Contracts::model()->getStatusFullList();
        $list[NULL]='All';
        return $list;
    }
    public function getData(){
        $crit=new CDbCriteria();
        $crit->addBetweenCondition('contractstartdate', $this->startDate, $this->endDate);
        $crit->compare('salesperson', $this->empno);
        $crit->compare('status', $this->status);
        $crit->compare('contractype', $this->type);
        $crit->order='contractstartdate,salesperson';
        $list=Contracts::model()->findAll($crit);
        
        $rs=array();
        
        foreach($list as $item){
            /* @var $item Contracts */
            $month=date('F',strtotime($item->contractstartdate));
            $year=date('Y',strtotime($item->contractstartdate));
            $rs[$year][$month][$item->salesperson][]=$item;
        
        }
        
        return $rs;
    }
    
}
?>
